define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: '',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                commonSearch:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id') ,operate:false},
                        {field: 'order_id', title: __('Order_id'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'),  operate:false},
                        {field: 'source', title: __('Source'), searchList: {"0":__('Source 0'),"1":__('Source 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('订单状态'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('已取消'),"8":__('已分配')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_money', title: __('Pay_money'), operate:false},
                        {field: 'bucket', title: __('Bucket'), searchList: {"0":__('Bucket 0'),"1":__('Bucket 1')}, formatter: Table.api.formatter.normal},
                        {field: 'delivery_type', title: __('Delivery_type'), searchList: {"0":__('Delivery_type 0'),"1":__('Delivery_type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'delivery_money', title: __('Delivery_money'), operate:'BETWEEN'},
                        {field: 'ticket_image', title: __('水票'), operate:'BETWEEN',operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'active_image', title: __('活动水票'),operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'delivery_time', title: __('Delivery_time'), operate: 'BETWEEN', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, 
                            events: Table.api.events.operate, 
                             buttons: [{
                                    name: 'detail',
                                    text: __('订单详情'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url:function(row){
                                        return 'orderdetail/index?order_sn=' + row.order_id;
                                    }
                                    
                                }],
                             formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 覆盖订单详情按钮的点击事件，使用 order_sn 而不是 id
           
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
